Closing Windows
The user closes a window either by clicking the window's close box (in the upper-left corner of the window) or by choosing the Close command from the File menu. To determine which window to close, you'll proceed in slightly different ways for these two cases. When the user clicks a window's close box, you can get a window pointer for that window by calling theFindWindow
function in response to the mouse-down event. When the user chooses a menu command, however, you can't do that; instead, you can call theFrontWindow
function to retrieve a pointer to the frontmost window on the screen.
When the user presses the mouse button while the cursor is in the close box, your application should call the
- Note
- You'll also want to close any windows that might be on the desktop when the user quits your application. You can do that by repeatedly calling
FrontWindow
until it returnsNIL
. See Listing 9-4 on page 175.![]()
TrackGoAway
function to track mouse movement until the user releases the button, as illustrated in Listing 6-13.Listing 6-13 Handling clicks in the close box
PROCEDURE DoGoAwayBox (myWindow: WindowPtr; mouseloc: Point); BEGIN IF TrackGoAway(myWindow, mouseloc) THEN DoCloseWindow(myWindow); END;IfTrackGoAway
returnsFALSE
, the user released the button while the cursor was outside the close box, and your application should do nothing. IfTrackGoAway
returnsTRUE
, your application should invoke its own procedure for closing a window.Listing 6-14 illustrates an application-defined function that closes a window. Notice that the effect of this function varies according to which kind of window it's being asked to close. If the user wants to close a dialog window,
DoCloseWindow
simply hides the window; this strategy leaves the data structures associated with the dialog box in memory, in expectation that the user might open the dialog box again. If the user wants to close a desk accessory window,DoCloseWindow
calls the Desk Manager routineCloseDeskAcc
to close that desk accessory.
PROCEDURE DoCloseWindow (myWindow: WindowPtr); BEGIN IF myWindow <> NIL THEN IF IsDialogWindow(myWindow) THEN {this is a dialog window} HideWindow(myWindow) ELSE IF IsDAccWindow(myWindow) THEN {this is a DA window} CloseDeskAcc(WindowPeek(myWindow)^.windowKind) ELSE IF IsAppWindow(myWindow) THEN {this is a document window} DoCloseDocWindow(myWindow); END;If the window to be closed is a document window,DoCloseWindow
calls the application-defined procedureDoCloseDocWindow
defined in Listing 6-15 to deallocate the document record, close the window, and then deallocate the window record.Listing 6-15 Closing a Venn diagram window
PROCEDURE DoCloseDocWindow (myWindow: WindowPtr); VAR myHandle: MyDocRecHnd; BEGIN IF myWindow = NIL THEN exit(DoCloseDocWindow) {ignore NIL windows} ELSE BEGIN myHandle := MyDocRecHnd(GetWRefCon(myWindow)); DisposeHandle(Handle(myHandle)); CloseWindow(myWindow); {close the window} DisposePtr(Ptr(myWindow)); {and release the storage} END; END;TheDoCloseDocWindow
procedure retrieves a handle to the document record from the window record. Then it callsDisposeHandle
to free the memory occupied by the document record. NextDoCloseDocWindow
closes the window by calling the Window Manager procedureCloseWindow
and deallocates the window record by callingDisposePtr
.
- Note
- When you create a window, if you allow the Window Manager to allocate memory for the window record (by passing
NIL
as the second parameter toGetNewWindow
), then you should call theDisposeWindow
procedure to close the window, instead of callingCloseWindow
andDisposePtr
.![]()